package com.team9.fitness.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.lang.reflect.Field;

/**
 * ZRDDS配置类
 * 负责ZRDDS中间件的配置和初始化
 */
@Configuration
public class ZRDDSConfig {

    private static final Logger log = LoggerFactory.getLogger(ZRDDSConfig.class);

    @Value("${zrdds.domain-id:0}")
    private int domainId;

    @Value("${zrdds.participant-name:FitnessParticipant}")
    private String participantName;

    @Value("${zrdds.qos-profile:udp_dp}")
    private String qosProfile;

    @Value("${zrdds.qos-file:ZRDDS_QOS_PROFILES.xml}")
    private String qosFile;

    /**
     * 初始化ZRDDS环境
     */
    @Bean
    public ZRDDSConfig initializeZRDDSEnvironment() {
        log.info("初始化ZRDDS环境...");

        try {
            // 设置ZRDDS库路径
            setupZRDDSLibraryPath();

            // 验证QoS配置文件
            validateQoSProfile();

            log.info("ZRDDS环境初始化完成");
            log.info("Domain ID: {}", domainId);
            log.info("Participant Name: {}", participantName);
            log.info("QoS Profile: {}", qosProfile);
            log.info("QoS File: {}", qosFile);

        } catch (Exception e) {
            log.error("ZRDDS环境初始化失败", e);
            throw new RuntimeException("ZRDDS环境初始化失败", e);
        }

        return this;
    }

    /**
     * 设置ZRDDS库路径
     */
    private void setupZRDDSLibraryPath() {
        try {
            // 获取项目根目录
            String projectRoot = System.getProperty("user.dir");
            String libPath = projectRoot + File.separator + "lib";

            // 验证库文件是否存在
            File zrddsJar = new File(libPath + File.separator + "ZRDDS.jar");
            File zrddsDll = new File(libPath + File.separator + "ZRDDS_JAVA.dll");

            if (!zrddsJar.exists()) {
                log.warn("ZRDDS.jar文件不存在: {}", zrddsJar.getAbsolutePath());
            } else {
                log.info("ZRDDS.jar文件存在: {}", zrddsJar.getAbsolutePath());
            }

            if (!zrddsDll.exists()) {
                log.warn("ZRDDS_JAVA.dll文件不存在: {}", zrddsDll.getAbsolutePath());
            } else {
                log.info("ZRDDS_JAVA.dll文件存在: {}", zrddsDll.getAbsolutePath());
            }

            // 设置库路径到系统属性
            String currentLibraryPath = System.getProperty("java.library.path");
            if (currentLibraryPath == null || !currentLibraryPath.contains(libPath)) {
                String newLibraryPath = currentLibraryPath == null ? libPath : currentLibraryPath + File.pathSeparator + libPath;
                System.setProperty("java.library.path", newLibraryPath);
                
                // 重新加载库路径
                try {
                    Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
                    fieldSysPath.setAccessible(true);
                    fieldSysPath.set(null, null);
                    log.info("已重新加载库路径: {}", newLibraryPath);
                } catch (Exception e) {
                    log.warn("无法重新加载库路径，可能需要重启JVM: {}", e.getMessage());
                }
            }

        } catch (Exception e) {
            log.error("设置ZRDDS库路径失败", e);
        }
    }

    /**
     * 验证QoS配置文件
     */
    private void validateQoSProfile() {
        try {
            // 检查QoS配置文件是否存在
            File qosFileObj = new File(qosFile);
            if (!qosFileObj.exists()) {
                log.warn("QoS配置文件不存在: {}", qosFile);
                log.info("将使用默认QoS配置");
            } else {
                log.info("QoS配置文件存在: {}", qosFileObj.getAbsolutePath());
            }

            // 检查QoS配置名称是否有效
            if ("udp_dp".equals(qosProfile)) {
                log.info("使用UDP传输协议的QoS配置: {}", qosProfile);
            } else {
                log.warn("未知的QoS配置名称: {}", qosProfile);
            }
        } catch (Exception e) {
            log.error("验证QoS配置文件失败", e);
        }
    }

    /**
     * 获取Domain ID
     */
    public int getDomainId() {
        return domainId;
    }

    /**
     * 获取Participant Name
     */
    public String getParticipantName() {
        return participantName;
    }

    /**
     * 获取QoS Profile
     */
    public String getQosProfile() {
        return qosProfile;
    }

    /**
     * 获取QoS File
     */
    public String getQosFile() {
        return qosFile;
    }
}
